1. /* dbl2lne.cpp by K.Tsuru */
  2. /****************************
  3. SN class's auxiliary function
  4. *****************************/
  5. #ifndef SN_H
  6. #include "sn.h"
  7. #endif
  8. /*-----------------------------------------------------------------------------
  9. Try to convert double "d" using long "L" into a form
  10. d = L * 10^e ( 0 =< |L| <= mt).
  11. When it is successed, return true. The operation between SDouble and double can
  12. be reduced into one between SDouble and short integer.
  13. e.g. x/1.3 ---> (x/13000)*DRADIX.
  14. Usually take mt = (double)d.SlOpMaxValue() (= ULONG_MAX/radix);
  15. -------------------------------------------------------------------------------*/
  16. bool doubleTolongExp(double d, long* L, int* e, double mt){
  17. // if(fabs(d) > mt) return false;
  18. if(d == 0.0){
  19. *L = 0; *e = 0; return true;
  20. }
  21. FigBlock res;
  22. uint sz;
  23. int exp, j;
  24. exp = doubleToArray(d, res, &sz);
  25. if(sz > 4) return false; // 0.0001 2345 6789 1000 (ten figures sz = 5) > SlOpMaxValue()
  26. double rdx = DRADIX, r = res(sz-1);
  27. for(j = (int)sz-2; j >= 1; j--){
  28. r += res(j)*rdx;
  29. rdx *= DRADIX;
  30. }
  31. exp = (exp - (int)sz +1)*DFIGURES;
  32. if(r > mt){ //It is desirable that "exp" is a multiple of "DFIGURES" if possible.
  33. while( fmod(r, 10.0) < DBL_EPSILON){ //remove lower zeros
  34. exp++; r /= 10.0;
  35. // if( !( abs(exp) % DFIGURES ) && (r < mt) ) break;
  36. }
  37. }
  38. *e = exp;
  39. r += ROUND_DBL_INT;
  40. if(r > mt) return false; //fail to convert
  41. // convert to long
  42. *L = (long)r;
  43. if(d < 0.0) *L = -*L;
  44. return true;
  45. }

dbl2lne.cpp : last modifiled at 2015/09/06 13:22:47(1,522 bytes)
created at 2016/04/11 11:17:20
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).